﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MMO_MANAGER.BUS;
using MMO_MANAGER.DTO;
using MMO_MANAGER.Utils;

namespace MMO_MANAGER.FORM_CATALOG
{
    public partial class frmNetworkList : DevExpress.XtraEditors.XtraForm
    {
        public frmNetworkList()
        {
            InitializeComponent();
        }

        network_list_bus bus;
        network_list nl;

        private void frmNetworkList_Load(object sender, EventArgs e)
        {
            bus = new network_list_bus();
            nl = new network_list();
            LoadCB();
            FillData();
        }

        private void FillData()
        {
            gridControl1.DataSource = bus.GetData();
        }

        private void btn_add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Add();
            FillData();
        }

        private void btn_edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit();
            FillData();
        }

        private void btn_delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Delete();
            FillData();
        }

        private void btn_refresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FillData();
        }

        private void btn_help_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        #region Add Edit Delete
        void Add()
        {
            try
            {
                using (var frm = new ChannelDlg())
                {
                    if (frm.ShowDialog(FindForm()) == DialogResult.OK)
                    {
                        nl.id_email = frm.EmailPrimary;
                        nl.id_gpage = frm.YoutubeAcc;
                        nl.ten = frm.Channel;
                        nl.id_label = frm.LabelName;
                        nl.id_ga = frm.EmailGA;
                        nl.ghichu = frm.Note;
                        nl.active = 1;    //  active
                        nl.ngayedit = DateTime.Now;

                        DataTable dt = bus.GetListChannelByName(nl);
                        if (dt.Rows.Count == 0)
                        {
                            bus.Insert(nl);
                            EasyDialog.ShowSuccessfulDialog("Thêm thành công!");
                            //lbl_totalrows.Text = gridView1.RowCount.ToString();
                        }
                        else
                        {
                            EasyDialog.ShowWarningDialog("Đã có Channel này rồi!");
                            return;
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                EasyDialog.ShowErrorDialog("Lỗi: " + ex.Message);
                return;
                throw;
            }
        }

        void Edit()
        {
            try
            {
                int row = gridView1.FocusedRowHandle;
                nl.id = Convert.ToInt16(gridView1.GetRowCellValue(row, "id"));
                nl.id_email = Convert.ToInt16(gridView1.GetRowCellValue(row, "id_email"));
                nl.id_gpage = Convert.ToInt16(gridView1.GetRowCellValue(row, "id_gpage"));
                nl.ten = gridView1.GetRowCellValue(row, "ten").ToString();
                nl.id_label = Convert.ToInt16(gridView1.GetRowCellValue(row, "id_label"));
                nl.id_ga = Convert.ToInt16(gridView1.GetRowCellValue(row, "id_ga"));
                nl.ghichu = gridView1.GetRowCellValue(row, "ghichu").ToString();

                using (var frm = new ChannelDlg())
                {
                    frm.EmailPrimary = nl.id_email;
                    frm.YoutubeAcc = nl.id_gpage;
                    frm.Channel = nl.ten;
                    frm.LabelName = nl.id_label;
                    frm.EmailGA = nl.id_ga;
                    frm.Note = nl.ghichu;

                    if (frm.ShowDialog(FindForm()) == DialogResult.OK)
                    {
                        nl.id_email = frm.EmailPrimary;
                        nl.id_gpage = frm.YoutubeAcc;
                        nl.ten = frm.Channel;
                        nl.id_label = frm.LabelName;
                        nl.id_ga = frm.EmailGA;
                        nl.ghichu = frm.Note;
                        nl.ngayedit = DateTime.Now;
                        bus.Update(nl);
                        EasyDialog.ShowSuccessfulDialog("Sửa thành công!");
                    }
                }
            }
            catch (Exception ex)
            {
                EasyDialog.ShowErrorDialog("Lỗi: " + ex.Message);
                return;
                throw;
            }
        }

        void Delete()
        {
            try
            {
                int row = gridView1.FocusedRowHandle;
                nl.id = Convert.ToInt16(gridView1.GetRowCellValue(row, "id"));
                DialogResult dr = EasyDialog.ShowYesNoDialog("Bạn có muốn xóa không?");
                if (dr == DialogResult.Yes)
                {
                    nl.active = 0;
                    bus.Delete(nl);
                    EasyDialog.ShowSuccessfulDialog("Xóa thành công!");
                }
            }
            catch (Exception ex)
            {
                EasyDialog.ShowErrorDialog("Lỗi: " + ex.Message);
                return;
                throw;
            }
        }

        void LoadCB()
        {
            // - Email
            email_list_bus bus_email = new email_list_bus();
            cbb_email_primary.DataSource = bus_email.GetData();
            cbb_email_primary.DisplayMember = "ten";
            cbb_email_primary.ValueMember = "id";
            // - Gpage - youtube
            gpage_list_bus bus_gpl = new gpage_list_bus();
            cbb_yt.DataSource = bus_gpl.GetData();
            cbb_yt.DisplayMember = "ten";
            cbb_yt.ValueMember = "id";
            // - Labe
            label_list_bus bus_lb = new label_list_bus();
            cbb_label.DataSource = bus_lb.GetData();
            cbb_label.DisplayMember = "ten";
            cbb_label.ValueMember = "id";
            // - GA
            ga_list_bus bus_ga = new ga_list_bus();
            cbb_ga.DataSource = bus_ga.GetData();
            cbb_ga.DisplayMember = "tenga";
            cbb_ga.ValueMember = "id";
        }
        #endregion

        private void btn_copy_email_Click(object sender, EventArgs e)
        {
            int row = gridView1.FocusedRowHandle;
            nl.ten = gridView1.GetRowCellValue(row, "ten").ToString();
            Clipboard.SetText(nl.ten);
            EasyDialog.ShowSuccessfulDialog("Copy successfull!");
        }

        private void btn_copy_pwd_Click(object sender, EventArgs e)
        {
            int row = gridView1.FocusedRowHandle;
            nl.ten = gridView1.GetRowCellValue(row, "pwd").ToString();
            //Clipboard.SetText(EasyEncoding.Descrypt(nl.ten, chuoi));
            EasyDialog.ShowSuccessfulDialog("Copy successfull!");
        }


    }
}